﻿// -------------------------------------------------------------------------
//    @FileName         :    NFSnsFriendUtil.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-04-01
//    @Email			:    445267987@qq.com
//    @Module           :    NFSnsFriendUtil
//
// -------------------------------------------------------------------------

#include "NFSnsFriendUtil.h"

#include <NFSnsOnlineRoleService.h>
#include <NFSnsFriendPart.h>
#include <NFSnsRoleDetail.h>
#include <NFComm/NFPluginModule/NFCheck.h>
#include <NFSnsServer/NFSnsCommon/NFSnsRoleSimpleService.h>

#include "proto_svr_common.pb.h"
#include "proto_svr_sns.pb.h"

int NFSnsFriendUtil::OnRoleLogin(NFSnsRoleDetail* pRoleDetail)
{
    CHECK_NULL(0, pRoleDetail);
    LoadFriendList(pRoleDetail);
    return 0;
}

int NFSnsFriendUtil::OnFriendQuery(NFSnsRoleDetail* pRoleDetail)
{
    CHECK_NULL(0, pRoleDetail);
    auto pFriendPart = pRoleDetail->GetPart<NFSnsFriendPart>();
    CHECK_NULL(pRoleDetail->GetRoleId(), pFriendPart);
    return 0;
}

int NFSnsFriendUtil::LoadFriendList(NFSnsRoleDetail* pRoleDetail)
{
    CHECK_NULL(0, pRoleDetail);
    auto pFriendPart = pRoleDetail->GetPart<NFSnsFriendPart>();
    CHECK_NULL(pRoleDetail->GetRoleId(), pFriendPart);
    auto pFriendList = pFriendPart->GetFriendInfo();
    CHECK_NULL(pRoleDetail->GetRoleId(), pFriendList);

    std::vector<uint64_t> vecRoleId;
    for (auto iter = pFriendList->myfriend.begin();iter != pFriendList->myfriend.end(); ++iter)
    {
        vecRoleId.push_back(iter->first);
    }

    for (auto iter = pFriendList->black.begin();iter != pFriendList->black.end(); ++iter)
    {
        vecRoleId.push_back(iter->first);
    }

    NFSnsRoleSimpleService::BatchLoadRoleSimpleInfo(pRoleDetail->GetRoleId(), vecRoleId, 0);
    return 0;
}

int NFSnsFriendUtil::OnBroadcastOtherFriend(NFSnsRoleDetail* pRoleDetail)
{
    CHECK_NULL(0, pRoleDetail);
    auto pFriendPart = pRoleDetail->GetPart<NFSnsFriendPart>();
    CHECK_NULL(pRoleDetail->GetRoleId(), pFriendPart);
    auto pFriendList = pFriendPart->GetFriendInfo();
    CHECK_NULL(pRoleDetail->GetRoleId(), pFriendList);
    NFSnsOnlineRole* pOnlineRole = NFSnsOnlineRoleService::GetOnlineRole(pRoleDetail->GetRoleId());
    CHECK_NULL(pRoleDetail->GetRoleId(), pOnlineRole);

    bool isHave = false;
    proto_ff::Proto_SGRoleLoginNtf ntf;
    ntf.set_target_role_id(pRoleDetail->GetRoleId());
    ntf.set_moudle(0);
    for (auto iter = pFriendList->other.begin(); iter != pFriendList->other.end(); ++iter)
    {
        NFSnsOnlineRole* pFriendRole = NFSnsOnlineRoleService::GetOnlineRole(iter->first);
        if(pFriendRole)
        {
            isHave = true;
            ntf.add_role_id(iter->first);
        }
    }

    if(isHave)
    {
        pOnlineRole->SendMsgToLogicServer(proto_ff::E_SG_ROLE_LOGIN_NTF, ntf);
    }

    return 0;
}

int NFSnsFriendUtil::OnBroadcastOtherFriendLogout(NFSnsRoleDetail* pRoleDetail)
{
    CHECK_NULL(0, pRoleDetail);
    auto pFriendPart = pRoleDetail->GetPart<NFSnsFriendPart>();
    CHECK_NULL(pRoleDetail->GetRoleId(), pFriendPart);
    return 0;
}
